package com.ruoyi.place.service.impl;

import java.util.List;

import com.ruoyi.baseCate.mapper.BaseCateMapper;
import com.ruoyi.city.mapper.CityMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.courses.mapper.CoursesMapper;
import com.ruoyi.provincial.mapper.ProvincialMapper;
import com.ruoyi.trip.mapper.TripMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.place.domain.Course;
import com.ruoyi.place.mapper.PlaceMapper;
import com.ruoyi.place.domain.Place;
import com.ruoyi.place.service.IPlaceService;

/**
 * 基地管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-16
 */
@Service
public class PlaceServiceImpl implements IPlaceService 
{
    private static final Logger log = LoggerFactory.getLogger(PlaceServiceImpl.class);
    @Autowired
    private PlaceMapper placeMapper;

    /**
     * 查询基地管理
     * 
     * @param id 基地管理主键
     * @return 基地管理
     */
    @Override
    public Place selectPlaceById(Long id)
    {
        return placeMapper.selectPlaceById(id);
    }

    /**
     * 查询基地管理列表
     * 
     * @param place 基地管理
     * @return 基地管理
     */
    @Override
    public List<Place> selectPlaceList(Place place)
    {
        return placeMapper.selectPlaceList(place);
    }


    @Autowired
    CityMapper cityMapper;
    @Autowired
    ProvincialMapper provincialMapper;
    /**
     * 新增基地管理
     * 
     * @param place 基地管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPlace(Place place)
    {
        String provincial = provincialMapper.selectProvincialById(place.getProId()).getProvincial();
        String city = cityMapper.selectCityById(place.getCitId()).getCity();
        if(provincial.equals(city)){
            place.setAddress(provincial);
        }else{
            place.setAddress(provincial+city);
        }
        //        补充分类名
        place.setCategory(baseCateMapper.selectBaseCateById(place.getBcid()).getName());
        System.out.println(baseCateMapper.selectBaseCateById(place.getBcid()).getName());
        place.setCreateTime(DateUtils.getNowDate());
        int row = placeMapper.insertPlace(place);
        log.debug(String.valueOf(place.getId()));
        insertCourse(place);
        return row;
    }


    @Autowired
    private TripMapper tripMapper;

    @Autowired
    private CoursesMapper coursesMapper;

    @Autowired
    private BaseCateMapper baseCateMapper;
    /**
     * 修改基地管理
     * 
     * @param place 基地管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePlace(Place place)
    {
        String provincial = provincialMapper.selectProvincialById(place.getProId()).getProvincial();
        String city = cityMapper.selectCityById(place.getCitId()).getCity();
        if(provincial.equals(city)){
            place.setAddress(provincial);
        }else{
            place.setAddress(provincial+city);
        }
        //        补充分类名
        place.setCategory(baseCateMapper.selectBaseCateById(place.getBcid()).getName());
        place.setUpdateTime(DateUtils.getNowDate());
        placeMapper.deleteCourseByBaseId(place.getId());
        insertCourse(place);
        int result= placeMapper.updatePlace(place);
        tripMapper.updateByTripId(place.getName(),place.getId());
        coursesMapper.updateByCoursesId(place.getName(),place.getId());
        return result;
    }

    /**
     * 批量删除基地管理
     * 
     * @param ids 需要删除的基地管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePlaceByIds(Long[] ids)
    {
        placeMapper.deleteCourseByBaseIds(ids);
        return placeMapper.deletePlaceByIds(ids);
    }

    /**
     * 删除基地管理信息
     * 
     * @param id 基地管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePlaceById(Long id)
    {
        placeMapper.deleteCourseByBaseId(id);
        return placeMapper.deletePlaceById(id);
    }

    /**
     * 新增课程管理信息
     * 
     * @param place 基地管理对象
     */
    public void insertCourse(Place place)
    {
        List<Course> courseList = place.getCourseList();
        Long id = place.getId();
        if (StringUtils.isNotNull(courseList))
        {
            List<Course> list = new ArrayList<Course>();
            for (Course course : courseList)
            {
                //        补充基地名
                course.setSubject(place.getName());
                course.setBaseId(id);
                list.add(course);
            }
            if (list.size() > 0)
            {
                placeMapper.batchCourse(list);
            }
        }
    }
}
